package com.mths.client.page;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
import com.google.gwt.user.client.ui.Widget;
import com.mths.client.component.Button;
import com.mths.client.component.Image;
import com.mths.client.controller.CompanyController;
import com.mths.client.controller.CompanyControllerAsync;
import com.mths.client.dto.CompanyLogoDTO;
import com.mths.client.util.AsyncCall;
import com.mths.client.util.ClientCacheUtil;
import com.mths.client.util.PageUtil;
import com.mths.client.util.UrlUtil;
import com.mths.client.widget.MessageBox;
import com.mths.client.widget.UploadFile;

public class CompanyLogoUpload extends AbstractPage implements IPage {

	private static CompanyLogoUiBinder	uiBinder	= GWT.create(CompanyLogoUiBinder.class);

	interface CompanyLogoUiBinder extends UiBinder<Widget, CompanyLogoUpload> {
	}

	private final CompanyControllerAsync	companyService	= GWT.create(CompanyController.class);
	CompanyLogoDTO							dto				= new CompanyLogoDTO();

	@UiField
	UploadFile								uploadFileCmp;
	@UiField
	Button									saveLogoBtn;
	@UiField
	Image									logoImg;

	public CompanyLogoUpload() {
		initWidget(uiBinder.createAndBindUi(this));
		initEventHandler();
		loadComponent();
	}

	private void loadComponent() {
		String companyOid = ClientCacheUtil.instance().getSessionUser().getSelectedCompanyOid();
		logoImg.addStyleName("maxH175");
		logoImg.setUrl(UrlUtil.generateLogoViewUrl(companyOid));

		saveLogoBtn.setEnabled(false);
	}

	private void initEventHandler() {
		uploadFileCmp.addCompletedCallback(new AsyncCallback<SubmitCompleteEvent>() {

			@Override
			public void onFailure(Throwable caught) {

			}

			@Override
			public void onSuccess(SubmitCompleteEvent result) {
				showUploadedImage();
				saveLogoBtn.setEnabled(true);
			}

		});

		uploadFileCmp.addFileChangedCallback(new AsyncCallback<ChangeEvent>() {

			@Override
			public void onFailure(Throwable caught) {
				saveLogoBtn.setEnabled(false);
			}

			@Override
			public void onSuccess(ChangeEvent result) {
				saveLogoBtn.setEnabled(false);
			}
		});

		saveLogoBtn.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				saveCompanyLogo();
			}

		});
	}

	private void showUploadedImage() {

	}

	private void saveCompanyLogo() {
		dto.setLogoFileSessionKey(uploadFileCmp.getFileSessionKey());
		companyService.logoUpload(dto, new AsyncCall<Void>() {
			@Override
			public void successCall(Void result) {
				PageUtil.dashboardHeader.renderCompanyLogo();
				new MessageBox("işleminiz başarılı olarak gerçekleştirilmiştir");
			}
		});
	}

	@Override
	public String pageName() {
		return messages.CompanyLogoUpload_pageName();
	}
}